<?php

/*$ip = $_SERVER['REMOTE_ADDR'];
if ($ip != '60.163.55.214') {
  //header( 'Location: http://dreamlife.co.nz/pz/tmp/dms1');
  header( 'Location: ../maintenance.html');
  return;
}*/

if (strpos(curPageURL(), 'www.') !== false) 
{
  header( 'Location: ' . str_replace('www.', '', curPageURL()));
  return;
}

function curPageURL() {
  $pageURL = 'http';
  $pageURL .= "://";
  if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  }
  
$firstpos=strpos($pageURL,"&u");
 if($firstpos !== false)
 {
 	$pageURL=substr($pageURL,0,$firstpos);
 }
  //echo $pageURL;
  return $pageURL;
}

 
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . '/includes/pdoorm.inc.php';

if (isset($_COOKIE[OPERATOR])) { 
  $_SESSION[OPERATOR] = json_decode($_COOKIE[OPERATOR]);
//print_r($_SESSION[OPERATOR]);
}

$path = MiscUtils::getParam('p', 'pagelogin') . '/';
$template = MiscUtils::getParam('t', 'pageuserlogin');

$menu = MiscUtils::getParam('m', json_encode(array()));
$supervise = MiscUtils::getParam('s', json_encode(array()));

$options = MiscUtils::getParam('opts', json_encode(array()));
$up = MiscUtils::getParam('u', json_encode(array()));
$client = MiscUtils::getParam('client', json_encode(array()));
$equrl = MiscUtils::getParam('callback_url', json_encode(array()));

//$options->client = $client;
//$options->equrl = $equrl;
$smarty->assign('client', $client);
$smarty->assign('equrl', $equrl);


$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[OPERATOR]);
  setcookie(OPERATOR, '', time() - 1, PATH, DOMAIN);
  $opts = json_encode(array());
  $up = json_encode(array());
  $smarty->assign('options', urldecode($opts));
  $smarty->assign('path', 'pagelogin/');
  $smarty->assign('template', 'pageuserlogin');
  $smarty->display("pagelogin/base.tpl");
  return; /* SignOut */
}
$_SESSION[OPERATOR] = new stdClass();
$_SESSION[OPERATOR]->type = USER_TYPE_ADMIN;

if (isset($_SESSION[OPERATOR])) {
	
$upstd = json_decode($up);
  if($upstd)
  {
        header( 'Location: ' . str_replace('www.', '', curPageURL()));
  }else{
		  if ($_SESSION[OPERATOR]->type == USER_TYPE_MANAGER_ASSISTANT) {
		    $path = 'pageasst/';
		    $usershort = 'MA';
		    $template = ($template == 'pageuserlogin') ? 'pageb1asstworks' : $template;
		    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract' && $template != 'pageproducehistory') {
		      $menu = new stdClass();
		      $menu->a = 1;
		      $menu->b = 1;
		      $menu->c = 1;
		      $menu->d = 1;
		      $menu = json_encode($menu);
		    }
		    if($supervise == json_encode(array())) {
		      $supervise = new stdClass();
		      $supervise->s = 0;
		      $supervise = json_encode($supervise);
		    }
		  } else if ($_SESSION[OPERATOR]->type == USER_TYPE_CUSTOMER_SERVICE) {
		    $path = 'pageserv/';
		    $usershort = 'CS';
		    $template = ($template == 'pageuserlogin') ? 'pageb1servworks' : $template;
		    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract' && $template != 'pageproducehistory') {
		      $menu = new stdClass();
		      $menu->a = 1;
		      $menu->b = 1;
		      $menu->c = 1;
		      $menu->d = 1;
		      $menu = json_encode($menu);
		    }
		    if($supervise == json_encode(array())) {
		      $supervise = new stdClass();
		      $supervise->s = 0;
		      $supervise = json_encode($supervise);
		    }
		  } else if ($_SESSION[OPERATOR]->type == USER_TYPE_MANAGER) {
		    $path = 'pagemanager/';
		    $usershort = 'MR';
		    $template = ($template == 'pageuserlogin') ? 'pageb1managercustomworks' : $template;
		    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract' && $template != 'pageproducehistory') {
		      $menu = new stdClass();
		      $menu->a = 1;
		      $menu->b = 1;
		      $menu->c = 1;
		      $menu->d = 1;
		      $menu = json_encode($menu);
		    }
		    if($supervise == json_encode(array())) {
		      $supervise = new stdClass();
		      $supervise->s = 1;
		      $supervise = json_encode($supervise);
		    }
		  } else if ($_SESSION[OPERATOR]->type == USER_TYPE_ADMIN) {
		  //header( 'Location: ' . str_replace('www.', '', curPageURL()));
		  //return;
		  	$path = 'pageadmin/';
		    $usershort = 'ADM';
		    $template = ($template == 'pageuserlogin') ? 'pageb1deletecus' : $template;
		    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract' && $template != 'pageproducehistory') {
		      $menu = new stdClass();
		      $menu->a = 1;
		      $menu->b = 1;
		      $menu->c = 1;
		      
		      $menu = json_encode($menu);
		    }
		  } else if ($_SESSION[OPERATOR]->type == USER_TYPE_ACCOUNTANT) {
		    $path = 'pageacct/';
		    $usershort = 'ACC';
		    $template = ($template == 'pageuserlogin') ? 'pageb1acctcustomworks' : $template;
		    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract' && $template != 'pageproducehistory') {
		      $menu = new stdClass();
		      $menu->a = 1;
		      $menu->b = 1;
		      $menu->c = 1;
		      $menu->d = 1;
		      $menu = json_encode($menu);
		    }
		  }
		  
		  $smarty->assign('userInfo', $_SESSION[OPERATOR]->account . ' (' . (((mb_substr($_SESSION[OPERATOR]->type, 0, 1) == '#') ? mb_substr($_SESSION[OPERATOR]->type, 1) : $usershort) . ')'));
		  
		  $smarty->assign('path', $path);
		  $smarty->assign('template', $template);
		
		  $smarty->assign('menu', json_decode($menu));
		  $smarty->assign('supervise', json_decode($supervise));
		  
		  $smarty->assign('operator', json_encode($_SESSION[OPERATOR]));
		  
		  $now = SimpleDate::fromStamp(time() + LAG);
		  $now->hours = 0;
		  $now->minutes = 0;
		  $now->seconds = 0;
		  $smarty->assign('now', json_encode($now));
		
		  if ($options == json_encode(array())) {
			  $nowStamp = SimpleDate::toStamp($now);
			  $opts = new stdClass();
			  $opts->template = $template;
			  $opts->nowstamp = $nowStamp;
			  $opts = json_encode($opts);
			  $smarty->assign('options', urldecode($opts));
			  
			  
		  } else {
		    $smarty->assign('options', urldecode($options));
		  }
		  $smarty->display($path . 'base.tpl');
		  return;
  }
} else {
  $upstd = json_decode($up);
  if($upstd)
  {
      $sql="select * from ns_v1user as d where d.account = '".$upstd->user."' AND d.password = '".$upstd->pass."'";
  
      $result=getall($sql);
      
      if (count($result) == 1) {
        $user = $result[0];
        $_SESSION[OPERATOR] = $user;
        
        setcookie(OPERATOR, json_encode($user), (time() + 3600), PATH, DOMAIN);
		
        header( 'Location: ' . str_replace('www.', '', curPageURL()));
      }else{//鐢ㄦ埛鍚嶅瘑鐮侀敊璇�      	  if(!empty($upstd->user) && !empty($upstd->pass))
      	  {
      	  	echo "<script>window.alert('Incorrect Account and/or Password');</script>";
      	  }  
      	
          $opts = new stdClass();
          $opts->template = $template;
          $opts->option = $options;
          $opts = json_encode($opts);
        
          $smarty->assign('options', urldecode($opts));
          $smarty->assign('up', urldecode($up));

		  /*echo $up."<br>";
		  echo $opts."<br>";
		  echo $t."<br>";
		  echo $m."<br>";  */ 
		  $returnurl =  curPageURL();       
          
		  $smarty->assign('returnurl', $returnurl);
          $smarty->assign('path', 'pagelogin/');
          $smarty->assign('template', 'pageuserlogin');
          $smarty->display("pagelogin/base.tpl");
          return; /* SignIn Required */     	
      }
  }else{	
	
		  $opts = new stdClass();
		  $opts->template = $template;
		  $opts->option = $options;
		  $opts = json_encode($opts);
		
		  $smarty->assign('options', urldecode($opts));
		  $smarty->assign('up', urldecode($up));
		  
		  /*echo $up."<br>";
		  echo $opts."<br>";
		  echo $t."<br>";
		  echo $m."<br>";  */ 
		  $returnurl ='';  
		
		  $smarty->assign('returnurl', $returnurl);
		  $smarty->assign('path', 'pagelogin/');
		  $smarty->assign('template', 'pageuserlogin');
		  $smarty->display("pagelogin/base.tpl");
		  return; /* SignIn Required */
  }
}
?>